#include "StdAfx.h"
#include "TaskPanelUI.h"
#include "BlueRenderEngineUI.h"
#include <Mmsystem.h>
#pragma comment(lib,"Winmm.lib")

CTaskPanelUI::CTaskPanelUI(void): m_hFadeBitmap(NULL)
{
	SetInset(CStdSize(8, 8));
	SetPadding(10);
	SetWidth(165);  // By default it gets a fixed 165 pixel width
}

CTaskPanelUI::~CTaskPanelUI(void)
{
	if( m_hFadeBitmap != NULL ) ::DeleteObject(m_hFadeBitmap);
	if( m_pManager != NULL ) m_pManager->KillTimer(this, FADE_TIMERID);
}

LPCTSTR CTaskPanelUI::GetClass() const
{
	return _T("TaskPanelUI");
}

SIZE CTaskPanelUI::EstimateSize(SIZE szAvailable)
{
	// The TaskPanel dissapears if the windows size becomes too small
	// Currently it is set to vasnish when its width gets below 1/4 of window.
	SIZE sz = m_cxyFixed;
	if( m_pManager->GetClientSize().cx <= m_cxyFixed.cx * 4 ) {
		// Generate a bitmap so we can paint this panel as slowly fading out.
		// Only do this when the control's size suddenly go below the threshold.
		if( m_rcItem.right - m_rcItem.left > 1 && m_hFadeBitmap == NULL ) {
			if( m_hFadeBitmap != NULL ) ::DeleteObject(m_hFadeBitmap);
			m_hFadeBitmap = CBlueRenderEngineUI::GenerateAlphaBitmap(m_pManager, this, m_rcItem, UICOLOR_DIALOG_BACKGROUND);
			// If we successfully created the 32bpp bitmap we'll set off the
			// timer so we can get animating...
			if( m_hFadeBitmap != NULL ) m_pManager->SetTimer(this, FADE_TIMERID, 50U);
			m_dwFadeTick = ::timeGetTime();
			m_rcFade = m_rcItem;
		}
		sz.cx = 1;
	}
	return sz;
}

void CTaskPanelUI::Event(TEventUI& event)
{
	if( event.Type == UIEVENT_TIMER && event.wParam == FADE_TIMERID )
	{
		// The fading animation runs for 500ms. Then we kill
		// the bitmap which in turn disables the animation.
		if( event.dwTimestamp - m_dwFadeTick > FADE_DELAY ) {
			m_pManager->KillTimer(this, FADE_TIMERID);
			::DeleteObject(m_hFadeBitmap);
			m_hFadeBitmap = NULL;
		}
		m_pManager->Invalidate(m_rcFade);
		return;
	}
	CVerticalLayoutUI::Event(event);
}

void CTaskPanelUI::SetPos(RECT rc)
{
	int cyFont = m_pManager->GetThemeFontInfo(UIFONT_NORMAL).tmHeight;
	RECT rcClient = { rc.left, rc.top + cyFont + 6, rc.right, rc.bottom };
	CVerticalLayoutUI::SetPos(rcClient);
	m_rcItem = rc;
}

void CTaskPanelUI::DoPaint(HDC hDC, const RECT& rcPaint)
{
	// Handling gracefull fading of panel
	if( m_hFadeBitmap != NULL ) {
		DWORD dwTimeDiff = ::timeGetTime() - m_dwFadeTick;
		TPostPaintUI job;
		job.rc = m_rcFade;
		job.hBitmap = m_hFadeBitmap;
		job.iAlpha = (BYTE) CLAMP(255 - (long)(dwTimeDiff / (FADE_DELAY / 255.0)), 0, 255);
		m_pManager->AddPostPaintBlit(job);
	}
	// A tiny panel (see explaination in EstimateSize()) is invisible
	if( m_rcItem.right - m_rcItem.left < 2) return;
	// Paint caption
	int cyFont = m_pManager->GetThemeFontInfo(UIFONT_NORMAL).tmHeight;
	RECT rcArc = { m_rcItem.left, m_rcItem.top, m_rcItem.right, m_rcItem.top + cyFont + 6 };
	CBlueRenderEngineUI::DoPaintArcCaption(hDC, m_pManager, rcArc, m_sText, UIARC_GRIPPER);
	// Paint background
	RECT rcClient = { m_rcItem.left, rcArc.bottom, m_rcItem.right, m_rcItem.bottom };
	COLORREF clrFirst, clrSecond;
	m_pManager->GetThemeColorPair(UICOLOR_TASK_BACKGROUND, clrFirst, clrSecond);
	CBlueRenderEngineUI::DoPaintGradient(hDC, m_pManager, rcClient, clrFirst, clrSecond, false, 128);
	CBlueRenderEngineUI::DoPaintFrame(hDC, m_pManager, rcClient, UICOLOR_TASK_CAPTION, UICOLOR_TASK_CAPTION, UICOLOR__INVALID, 0);
	// Paint elements
	CVerticalLayoutUI::DoPaint(hDC, rcPaint);
}

